﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using ARR.UI.CountryService;
using ARR.UI.RangeReviewsService;
using System.Collections.ObjectModel;
using ARR.Entities;
using Telerik.Windows.Controls;
using System.Linq;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class LoginView : Window
    {
        public LoginView()
        {
            this.InitializeComponent();
            this.DataContext = this;

            //StyleManager.SetTheme(this.ddlCountries, new TransparentTheme());
            //StyleManager.SetTheme(this.ddlRangeReviews, new TransparentTheme());

            CenterWindowOnScreen();

            Load();
        }


        public ObservableCollection<Country> Countries { get; set; }

        public ObservableCollection<RangeReview> RangeReviews { get; set; }

        public Country SelectedCountry { get; set; }

        public RangeReview SelectedRangeReview { get; set; }



        private void Load()
        {
            CountryServiceClient service = new CountryServiceClient();
            try
            {
                Countries = new ObservableCollection<Country>(service.GetAll().OrderBy(x => x.Name));
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }

        private void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string errorMsg = null;

            if (SelectedCountry == null)
            {
                errorMsg = "Por favor seleccione un País ";

                if (SelectedRangeReview == null)
                {
                    errorMsg += "y un Range Review del mismo para ingresar.";
                }
            }
            else if (SelectedRangeReview == null)
            {
                errorMsg = "Por favor seleccione un Range Review para ingresar ";
            }
            else if (SelectedRangeReview.Id == 0)
            {
                int rangeNum = RangeReviews.Count;
                RangeReview newRange = new RangeReview
                {
                    Id = 0,
                    Title = string.Format("{0} {1}", SelectedCountry.Name, rangeNum),
                    FirstMonth = new DateTime(DateTime.Now.Year, 1, 1),
                    TotalMonths = 6,
                    HasPriorYear = true,
                    CountryId = SelectedCountry.Id,
                    TargetCurrencyId = 5,
                    ReportDate = DateTime.Today,
                    Bottom = 5,
                    RangeReviewStores = new List<RangeReviewStore>(),
                    Benchmarks = new List<RangeReviewBenchmark>(),
                    RangeReviewSetups = null
                };

                RangeReviewsServiceClient service = new RangeReviewsServiceClient();
                try
                {
                    SelectedRangeReview = service.SaveNewAndGetIt(newRange);
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }

            }

            if (errorMsg != null)
            {
                lblError.Text = errorMsg;
            }
            else
            {
                WorkspaceData.Instance.Country = SelectedCountry;
                WorkspaceData.Instance.RangeReview = SelectedRangeReview;


                MainWindow main = new MainWindow();
                main.Show();

                this.Close();
            }
        }

        private void ddlCountries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RangeReviewsServiceClient service = new RangeReviewsServiceClient();
            try
            {
                RangeReviews = new ObservableCollection<RangeReview>(service.GetAll(SelectedCountry.Id, true));
                RangeReviews.Add(new RangeReview() { Id = 0, Title = "--Nuevo Range Review--" });

                ddlRangeReviews.ItemsSource = RangeReviews;
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }

        }

        private void CenterWindowOnScreen()
        {
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            double windowWidth = this.rootLayout.Width;
            double windowHeight = this.rootLayout.Height;

            double x = (screenWidth / 2) - (windowWidth / 2);
            double y = (screenHeight / 2) - (windowHeight / 2);

            TranslateTransform translate = new TranslateTransform(x, y);

            this.rootLayout.RenderTransform = translate;

        }

    }
}